package filter;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import filter.StringWrapper;

/**
 * Servlet Filter implementation class FooterFilter
 */
@WebFilter("/INACTIF_NOW")
public class PresenterFilter implements Filter {

    /**
     * Default constructor. 
     */
    public PresenterFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}
	
	
	public String getHeader(ServletRequest request){
		String header =
		"<div id=\"banner\">"+
		"<img src=\"/Derpizza/img/derpina.jpg\"/>";
		
		if(((HttpServletRequest)request).getSession().getAttribute("nomUser")==null){
			header += "<p>Bienvenue, nous sommes le "+new SimpleDateFormat().format(new Date())+"</p>"+
			"<br/><a href=\"/Derpizza/connexion.jsp\">Se Connecter</a>";
		}else {
			header += "<p>Bienvenue, "
					+((HttpServletRequest)request).getSession().getAttribute("nomUser")+
					" nous sommes le "+new SimpleDateFormat().format(new Date())+"</p>"+
					"<br/><a href=\"/Derpizza/SeDeconnecter\">Se Deconnecter</a>";		
		}
		header += "</div>";
		
		return header;
	}
	
	public String getFooter(ServletRequest request){
		String footer = "<div id=\"footer\">"+
							"<p>Copyright Derpizza 2012</p>"+
						"</div>";		
		
		return footer;
	}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		//Capture de la requete
		StringWrapper responseBody = new StringWrapper((HttpServletResponse)response);
		chain.doFilter((HttpServletRequest)request, responseBody);
		
		//Injection du header juste apres le <body>		
		
		//Composition de la page
		String reponse = responseBody.toString().replace("<body>", "<body>"+getHeader(request));
		reponse = reponse.toString().replace("</body>", getFooter(request)+"</body>");
		
		response.getWriter().append(reponse);
	}
	
	

	//public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		//RequestDispatcher dispatcher; 
		//Inclusion de l'entete
		//dispatcher = request.getRequestDispatcher("/Banner");
		//Si la reponse existe, je peux afficher le pied de page
		//if(dispatcher!=null){
		//	dispatcher.include(request, response);
		//}
		
		//Capture de la requete
		//StringWrapper responseWrapper = new StringWrapper((HttpServletResponse)response);
		//chain.doFilter((HttpServletRequest)request, responseWrapper);
		
		
		//Ici ne modifie pas la reqete de la page suivante,
		//on peut donc la laisser s'afficher sans utiliser un String wrapper	
		//chain.doFilter(request, response);
		
		//Si la reponse existe, je peux afficher le pied de page
		//dispatcher = request.getRequestDispatcher("/Footer");
		//if(dispatcher!=null){
		//	dispatcher.include(request, response);
		//}
	//}
	
	

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
